/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.credsoft.copcred.model.vo;

import br.gov.caixa.gisutsa.framework.tapiocaframework.persistencia.AbstractEntity;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 *
 * @author Sales
 * @since 2009-05-29
 */
@Entity
@Table(name = "perfil")
public class Perfil extends AbstractEntity {

    private static final long serialVersionUID = 7493929007435078764L;
    private Long id;
    private int versao;
    private String nome;
    private String descricao;
    private List<Usuario> usuarios;
    private List<Menu> menus;

    public Perfil() {
        usuarios = new LinkedList<Usuario>();
    }

    public Perfil(Long id) {
        this();
        this.id = id;
    }

    public Perfil(Long id, String nome, String descricao) {
        this(id);
        this.nome = nome;
        this.descricao = descricao;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "perfil_id", nullable = false)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Version
    @Column(name = "versao", nullable = false)
    public int getVersao() {
        return versao;
    }

    public void setVersao(int versao) {
        this.versao = versao;
    }

    @Column(name = "nome", nullable = false, length = 25)
    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    @Column(name = "descricao", nullable = false, length = 50)
    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    @OneToMany(targetEntity = Usuario.class, mappedBy = "perfil")
    public List<Usuario> getUsuarios() {
        return usuarios;
    }

    public void setUsuarios(List<Usuario> usuarios) {
        this.usuarios = usuarios;
    }

    @ManyToMany(targetEntity = Menu.class, fetch = FetchType.LAZY)
    @JoinTable(name = "permissao",
    joinColumns = {
        @JoinColumn(name = "perfil_id", nullable = false)
    },
    inverseJoinColumns = {
        @JoinColumn(name = "menu_id", nullable = false)
    })
    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(List<Menu> menus) {
        this.menus = menus;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Perfil other = (Perfil) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 83 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    public void addUsuario(Usuario usuario) {
        this.usuarios.add(usuario);
    }

    public void removeUsuario(Usuario usuario) {
        this.usuarios.remove(usuario);
    }
}
